<template>
  <div class="user-profile" v-if="user">
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      right-text="保存"
      left-arrow
      @click-left="$router.back()"
    />

    <input type="file" hidden ref="file" @change="onFileChange" />
    <van-cell
      class="photo-cell"
      title="头像"
      is-link
      @click="$refs.file.click()"
    >
      <van-image class="avator" :src="user.photo" round center fit="cover">
      </van-image>
    </van-cell>

    <van-cell title="昵称" is-link :value="user.name" />
    <van-cell
      title="性别"
      is-link
      :value="user.gender === 0 ? '男' : '女'"
      @click="isUpdateGenderShow = true"
    />
    <van-cell
      title="生日"
      is-link
      :value="user.birthday"
      @click="isUpdateBirthdayShow = true"
    />

    <van-popup
      v-model="isUpdatePhotoShow"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <updata-photo
        v-if="isUpdatePhotoShow"
        :img="img"
        @close="isUpdatePhotoShow = false"
        @update-photo="user.photo = $event"
      ></updata-photo>
    </van-popup>

    <!-- 性别遮罩层 -->
    <van-popup
      v-if="isUpdateGenderShow"
      v-model="isUpdateGenderShow"
      position="bottom"
      :style="{ height: '50%' }"
    >
      <update-gender
        @close="isUpdateGenderShow = false"
        v-model="user.gender"
      ></update-gender>
    </van-popup>

    <!-- 生日遮罩层 -->
    <van-popup
      v-if="isUpdateBirthdayShow"
      v-model="isUpdateBirthdayShow"
      position="bottom"
      :style="{ height: '60%' }"
    >
      <update-birthday
        @close="isUpdateBirthdayShow = false"
        v-model="user.birthday"
      ></update-birthday>
    </van-popup>
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user'
import UpdataPhoto from './components/updata-photo.vue'
import UpdateGender from './components/update-gender.vue'
import UpdateBirthday from './components/update-birthday.vue'
export default {
  name: 'userProfile',
  components: {
    UpdataPhoto,
    UpdateGender,
    UpdateBirthday
  },
  props: {},
  data() {
    return {
      user: null,
      isUpdatePhotoShow: false,
      isUpdateGenderShow: false, // 控制性别遮罩层
      isUpdateBirthdayShow: false, // 生日遮罩层开关
      img: null
    }
  },
  computed: {},
  watch: {},
  created() {
    this.loadUserProfile()
  },
  mounted() {},
  methods: {
    async loadUserProfile() {
      try {
        const { data } = await getUserProfile()
        this.user = data.data
      } catch (error) {}
    },
    onFileChange() {
      console.log('changed')
      const file = this.$refs.file.files[0]
      console.log('file:', file)

      // 基于图片对象获取 blob 数据
      this.img = window.URL.createObjectURL(file)
      console.log('img:', this.img)
      this.isUpdatePhotoShow = true
      // 清空上传图片input控件的值
      this.$refs.file.value = ''
    }
  }
}
</script>

<style lang="less" scoped>
.user-profile {
  .van-nav-bar {
    background-color: #3296fa;
    /deep/ .van-icon {
      color: #fff;
    }
    /deep/ .van-nav-bar__title {
      color: #fff;
    }
    /deep/ .van-nav-bar__text {
      color: #fff;
    }
  }
  .photo-cell {
    .van-cell__value {
      top: -6px;
    }
    .avator {
      width: 60px;
      height: 60px;
    }
  }
}
</style>
